home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 201 / 201.xpi / modules / prompts.jsm < prev    next >
Encoding:
Text File  |  2010-01-11  |  3.0 KB  |  130 lines

  1. /* You may find the license in the LICENSE file */
  2.  
  3. var EXPORTED_SYMBOLS = ['confirm', 'confirmOC', 'confirmYN', 'alert'];
  4.  
  5. const Cc = Components.classes;
  6. const Ci = Components.interfaces;
  7. const Cr = Components.results;
  8.  
  9. // unpack the default button types
  10. for (let x in Components.interfaces.nsIPromptService) {
  11.     let r = new String(x).match(/BUTTON_TITLE_(\w+)$/);
  12.     if (r) {
  13.         this[r[1]] = Components.interfaces.nsIPromptService[x];
  14.         EXPORTED_SYMBOLS.push(r[1]);
  15.     }
  16. }
  17.  
  18. /**
  19.  * wrapper around confirmEx
  20.  * @param title. Dialog title
  21.  * @param text. Dialog text
  22.  * @param button0. Either null (omit), one of CANCEL/NO/... or a string
  23.  * @param button1. s.a.
  24.  * @param button2. s.a.
  25.  * @param default. Index of the Default button
  26.  * @param check. either null, a boolean, or string specifying the prefs id.
  27.  * @param checkText. The text for the checkbox
  28.  * @return Either the button# or {button: #, checked: bool} if check was a boolean
  29.  * @author Nils
  30.  */
  31. function confirm(aWindow, aTitle, aText, aButton0, aButton1, aButton2, aDefault, aCheck, aCheckText) {
  32.     let prompts = Cc["@mozilla.org/embedcomp/prompt-service;1"]
  33.         .getService(Ci.nsIPromptService);
  34.     
  35.     // Set up the flags/buttons
  36.     let flags = 0;
  37.     [aButton0, aButton1, aButton2].forEach(
  38.         function(button, idx) {
  39.             if (typeof button == 'number') {
  40.                 flags += prompts['BUTTON_POS_' + idx] * button;
  41.                 button = null;
  42.             }
  43.             else if (typeof button == 'string' || button instanceof String) {
  44.                 flags |= prompts['BUTTON_POS_' + idx] * prompts.BUTTON_TITLE_IS_STRING;
  45.             }
  46.             else {
  47.                 button = 0;
  48.             }
  49.         },
  50.         this
  51.     );
  52.     if (aDefault == 1) {
  53.         flags += prompts.BUTTON_POS_1_DEFAULT;
  54.     }
  55.     else if (aDefault == 2) {
  56.         flags += prompts.BUTTON_POS_2_DEFAULT;
  57.     }
  58.     
  59.     // Checkmark requested?
  60.     let rv = null;
  61.     let check = {};
  62.     if (aCheckText) {
  63.         if (typeof(aCheck) == 'boolean') {
  64.             rv = {};
  65.             check.value = aCheck;
  66.         }
  67.         else if (typeof(aCheck) == 'string' || aCheck instanceof String) {
  68.             check.value = undefined;
  69.             try {
  70.                 check.value = Cc['@mozilla.org/preferences-service;1']
  71.                     .getService(Ci.nsIPrefBranch)
  72.                     .getBoolPref(aCheck);
  73.             }
  74.             catch (ex) {
  75.                 // no-op                
  76.             }
  77.             if (check.value == undefined) {
  78.                 check.value = false;
  79.             }
  80.         }
  81.     }
  82.     
  83.     let cr = prompts.confirmEx(
  84.         aWindow,
  85.         aTitle,
  86.         aText,
  87.         flags,
  88.         aButton0,
  89.         aButton1,
  90.         aButton2,
  91.         aCheckText,
  92.         check
  93.     );
  94.     
  95.     // We've got a checkmark request
  96.     if (rv) {
  97.         rv.checked = check.value;
  98.         rv.button = cr;
  99.         return rv;
  100.     }
  101.     
  102.     // Just return as usual
  103.     return cr;
  104. }
  105.  
  106. /**
  107.  * Shortcut for OK/Cancel Confirmation dialogs
  108.  * @author Nils
  109.  */
  110. function confirmOC(aWindow, aTitle, aText) {
  111.     return confirm(aWindow, aTitle, aText, OK, CANCEL);
  112. }
  113.  
  114. /**
  115.  * Shortcut for Yes/No Confirmation dialogs
  116.  * @author Nils
  117.  */
  118. function confirmYN(aWindow, aTitle, aText) {
  119.     return confirm(aWindow, aTitle, aText, YES, NO);
  120. }
  121.  
  122. /**
  123.  * wrapper around alert
  124.  * @author Nils
  125.  */
  126. function alert(aWindow, aTitle, aText) {
  127.     Cc["@mozilla.org/embedcomp/prompt-service;1"]
  128.         .getService(Ci.nsIPromptService)
  129.         .alert(aWindow, aTitle, aText);
  130. }